package com.projet.entity.hero;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

import com.project.Constants;
import com.projet.entity.IEntity;
import com.projet.entity.RenderComponent;
import com.sun.corba.se.impl.orbutil.closure.Constant;

public class ComposantGraphiqueHero extends RenderComponent {
	
	SpriteSheet sprites;
	Animation gauche,droite, haut, bas,action, spriteCourant;
	
	public static final int duration=100;
	public static final boolean horizontalScan=true;
	public static final boolean autoUpdate=false;
	public static final int decalageX=4;
	public static final int decalageY=10;
	public static final int tempsAttaque=200;
	
	boolean attacking=false, moving=false;
	int deltaAttaque=0;
	
	int direction=3;
	
	public ComposantGraphiqueHero() throws SlickException {
		super();
		sprites = new SpriteSheet("res/images/link24x33.png",24,33);
		
		gauche=new Animation(sprites,0,3,2,3,horizontalScan,duration,autoUpdate);
		droite=new Animation(sprites,0,1,2,1,horizontalScan,duration,autoUpdate);
		haut=new Animation(sprites,0,0,2,0,horizontalScan,duration,autoUpdate);
		bas=new Animation(sprites,0,2,2,2,horizontalScan,duration,autoUpdate);
		action=new Animation(sprites,3,0,5,0,horizontalScan,duration,true);
		
		
		spriteCourant=droite;
	}
	
public ComposantGraphiqueHero(String id, IEntity e) throws SlickException {
		super(id,e);
sprites = new SpriteSheet("res/images/link24x33.png",24,33);
		
		gauche=new Animation(sprites,0,3,2,3,horizontalScan,duration,autoUpdate);
		droite=new Animation(sprites,0,1,2,1,horizontalScan,duration,autoUpdate);
		haut=new Animation(sprites,0,0,2,0,horizontalScan,duration,autoUpdate);
		bas=new Animation(sprites,0,2,2,2,horizontalScan,duration,autoUpdate);
		action=new Animation(sprites,3,0,5,0,horizontalScan,duration,autoUpdate);
		
		spriteCourant=droite;
		
	}
	
	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta) {
		Input input = gc.getInput();
        moving=false;
		if(!attacking)
		{
			if (input.isKeyDown(Input.KEY_UP))
	        {
	        	spriteCourant=haut;
	        	haut.update(delta);
	        	this.direction= Constants.Nord;
	        	moving=true;
	        }
	        
	        else if (input.isKeyDown(Input.KEY_DOWN))
	        {
	        	spriteCourant=bas;
	        	bas.update(delta);
	        	this.direction=Constants.Sud;
	        	moving=true;
	        }
	        else if (input.isKeyDown(Input.KEY_LEFT))
	        {
	        	spriteCourant=gauche;
	        	gauche.update(delta);
	        	this.direction=Constants.Ouest;
	        	moving=true;
	        }
	        else if (input.isKeyDown(Input.KEY_RIGHT))
	        {
	        	spriteCourant=droite;
	        	droite.update(delta);
	        	this.direction=Constants.Est;
	        	moving=true;
	        }
	         if (input.isKeyDown(Input.KEY_ENTER))
	        {
	        	spriteCourant=action;
	        }
	         switch (direction)
	         {
	         case Constants.Nord : spriteCourant=haut; break;
	         case Constants.Sud : spriteCourant=bas; break;
	         case Constants.Est : spriteCourant=droite; break;
	         case Constants.Ouest : spriteCourant=gauche; break;
	         }
	         //Si le joueur ne bouge pas, alors on place le sprite courant sur l'image "debout immobile" (et pas "en train de faire un pas")
	         if(!moving)
	         {
	        	 spriteCourant.setCurrentFrame(1);
	         }
		}
		
		else
         {
        	 if (deltaAttaque>200)
    		 {
    			 attacking=false;
    		 }
        	 else
        	 {
        		 spriteCourant.update(delta);
        		 
        	 }
         }
         if (input.isKeyPressed(Input.KEY_SPACE))
         {
        	 
        	 if (attacking==false)
        	 {
        		 deltaAttaque=0;
        		 attacking=true;
        		 spriteCourant = ((ComposantArmeEpee) this.getOwnerEntity().getComponent(Constants.ComposantArme)).attaquer(direction);
        		 ((ComposantArmeEpee) this.getOwnerEntity().getComponent(Constants.ComposantArme)).playSound();
        	 }        	 
         }
         
         deltaAttaque=deltaAttaque+delta;
        
	}
	
	@Override
	public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
		spriteCourant.draw(owner.getPosition().getX()-decalageX, owner.getPosition().getY()-decalageY);
	}
	

}
